﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class City_Entity : EntityBase<CITY>
    {
        public override RESP_Info Add(CITY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into CITY(CITY_ID,CITY_NAME,BELONG) values (@CITY_ID,@CITY_NAME,@BELONG)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CITY_ID", model.CITY_ID));
                    cmd.Parameters.Add(new SqlParameter("CITY_NAME", model.CITY_NAME));
                    cmd.Parameters.Add(new SqlParameter("BELONG", model.BELONG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加城市成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加城市失败</p></h5>";
                    info.Url = "CityAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加城市失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(CITY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update CITY set BELONG=@BELONG,CITY_NAME=@CITY_NAME where CITY_ID=@CITY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CITY_ID", model.CITY_ID));
                    cmd.Parameters.Add(new SqlParameter("CITY_NAME", model.CITY_NAME));
                    cmd.Parameters.Add(new SqlParameter("BELONG", model.BELONG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑城市成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑城市失败</p></h5>";
                    info.Url = "CityAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑城市失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(CITY city)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from CITY where CITY_ID=@CITY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CITY_ID", city.CITY_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除城市成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除城市失败</p></h5>";
                    info.Url = "CityList.aspx";

                    Tom.Net.Log.LogManager.Error("删除城市失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override CITY GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select CITY_ID, CITY_NAME,BELONG from CITY where CITY_ID=@CITY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CITY_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CITY city = new CITY();
                        if (!reader.IsDBNull(0)) city.CITY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) city.CITY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) city.BELONG = reader.GetString(2);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return city;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取城市数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override CITY GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CITY city = new CITY();
                        if (!reader.IsDBNull(0)) city.CITY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) city.CITY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) city.BELONG = reader.GetString(2);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return city;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取城市数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<CITY> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<CITY> citys = new List<CITY>();
                    while (reader.Read())
                    {
                        CITY city = new CITY();
                        if (!reader.IsDBNull(0)) city.CITY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) city.CITY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) city.BELONG = reader.GetString(2);

                        citys.Add(city);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return citys;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取城市数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
